package com.hc.fdkforwarders.common.utils;

import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author liuyuqing
 * @Description Object 转型工具类
 */
public class ObjectTransformUtils {

    /**
     * 转型为字符串
     */
    public static String transformToStringDefaultNull(Object param) {
        if (param == null) {
            return null;
        }
        return param.toString();
    }

    /**
     * 转型为非空字符串，默认值为空串
     */
    public static String transformToNonNullStringDefaultEmpty(Object param) {
        if (param == null) {
            return "";
        }
        return param.toString();
    }

    /**
     * 转型为非空Integer，默认值为0
     */
    public static Integer transformToIntegerDefault0(Object param) {
        Integer result = transformToIntegerDefaultNull(param);
        if (result == null) {
            return 0;
        }
        return result;
    }

    /**
     * 转型为Integer，默认值为null
     */
    public static Integer transformToIntegerDefaultNull(Object param) {
        if (param == null) {
            return null;
        }
        if (param instanceof Integer) {
            return (Integer) param;
        }
        String string = param.toString();
        //正则表达式，匹配非负整数
        String regEx = "^-?[0-9]\\d*$";
        //判断字符串是否与正则表达式相匹配
        if (stringCheck(string, regEx)) {
            return Integer.valueOf(string);
        }
        return null;
    }

    /**
     * 转型为非空Long，默认值为0
     */
    public static Long transformToLongDefault0(Object param) {
        Long result = transformToLongDefaultNull(param);
        if (result == null) {
            return 0L;
        }
        return result;
    }

    /**
     * 转型为非空Long，默认值为null
     */
    public static Long transformToLongDefaultNull(Object param) {
        if (param == null) {
            return null;
        }
        if (param instanceof Long) {
            return (Long) param;
        }
        String string = param.toString();
        //正则表达式，匹配非负整数
        String regEx = "^-?[0-9]\\d*$";
        //判断字符串是否与正则表达式相匹配
        if (stringCheck(string, regEx)) {
            return Long.valueOf(string);
        }
        return null;
    }

    /**
     * 判断字符串是否匹配正则表达式
     */
    private static boolean stringCheck(String str, String regEx) {
        Pattern pattern = Pattern.compile(regEx);
        Matcher matcher = pattern.matcher(str);
        return matcher.matches();
    }

    /**
     * 转型为非负非空小数，默认值为0
     */
    public static BigDecimal transformToNonNegativeBigDecimalDefault0(Object param) {
        if (param == null) {
            return new BigDecimal("0");
        }
        if (param instanceof BigDecimal) {
            return (BigDecimal) param;
        }
        if (param instanceof Double) {
            return BigDecimal.valueOf((Double) param);
        }
        String string = param.toString();
        //正则表达式，匹配非负小数
        String regEx = "^\\d+(\\.\\d+)?$";
        if (stringCheck(string, regEx)) {
            return new BigDecimal(string);
        }
        return new BigDecimal("0");
    }
}
